package com.kichina.parking.module.message.socket;

import io.vertx.core.Handler;
import io.vertx.core.net.NetSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

/**
 * @author : <a href="https://iiiii.run">xiaohan.yuan</a>
 * @version :    1.0
 * @description :  socket的处理类
 * @date :    2021-09-10 18:07
 */
@Component
@Scope(value = SCOPE_PROTOTYPE,proxyMode = ScopedProxyMode.TARGET_CLASS)
@Slf4j
public class SocketHandler implements Handler<NetSocket> {

    private final SocketMessageHandler socketMessageHandler;

    private final SocketCloseHandler socketCloseHandler;

    public SocketHandler(SocketCloseHandler socketCloseHandler, SocketMessageHandler socketMessageHandler) {
        this.socketCloseHandler = socketCloseHandler;
        this.socketMessageHandler = socketMessageHandler;
    }

    /**
     * 收到socket的连接
     * @param socket socket
     * @author <a href="https://iiiii.run">xiaohan.yuan</a>
     */
    @Override
    public void handle(NetSocket socket) {
        socket.handler(buffer-> socketMessageHandler.handle(buffer,socket));
        socket.closeHandler(s->socketCloseHandler.handler(socket));
    }
}
